package org.homeworkteam.controller;


import org.homeworkteam.bean.FavoriteVideo;
import org.homeworkteam.bean.Users;
import org.homeworkteam.bean.Video;
import org.homeworkteam.dto.ResponseData;
import org.homeworkteam.service.AdminService;
import org.homeworkteam.service.UserService;
import org.homeworkteam.service.VideoService;
import org.homeworkteam.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    public static String uploadDir = "E:\\files";
    @Autowired
    private UserService userService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private AdminService adminService;
    /**
     * 拍照搜题接口
     * type 1 图片批改   2 中文作文批改  3 英语作文批改  4 翻译  5 儿童绘画批改  6  ai写作
     * @param file
     * @param redirectAttributes
     * @return filePath文件路径
     */
    @PostMapping("/answerQuestion")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {

        System.out.println("file:" + file);
        System.out.println("文件原始名称: " + file.getOriginalFilename());

        try {
            String filePath = FileUploadUtils.saveFile(file, uploadDir);
            redirectAttributes.addFlashAttribute("message", "文件上传成功");
            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("文件上传失败: " + e.getMessage());
            redirectAttributes.addFlashAttribute("message", "文件上传失败: " + e.getMessage());
        }

        return "faild"; // 返回重定向到结果页面
    }

    /**
     * 登录接口
     * @param users
     * @return
     */
    @PostMapping("/login")
    public ResponseData getUserInfo(@RequestBody Users users) {
        System.out.println("username: " + users.getUserAccount());
        System.out.println("password: " + users.getUserPassword());
//
//        System.out.println("users: " + users);
//        System.out.println(userService.getUserInfo(users));
        Users user = userService.getUserInfo(users);
        if (user == null){
            return new ResponseData(400, "用户不存在");
        }
        List<Integer> purchased = videoService.getPurchasedVideosByUserId(user.getUserId());
        List<FavoriteVideo> favorite = videoService.getFavoriteVideosByUserId(user.getUserId());
        System.out.println(purchased+"啊啊??"+favorite);

        // 填充到用户对象
        user.setPurchased(purchased);
        user.setFavorite(favorite);
        return new ResponseData(200, "ok",user);
    }

    /**
     *
     * 注册接口
     * @param users
     * @return
     */
    @PostMapping("/register")
    public ResponseData addUser(@RequestBody Users users) {
        System.out.println("users: " + users);
        Users user = userService.checkUser(users.getUserAccount());
        if (user != null){
            return new ResponseData(400, "用户已存在");
        }else {
            int result = userService.addUser(users);
            return new ResponseData(200, "ok",result);
        }

    }
    @PostMapping("/change")
    public ResponseData changeUser(@RequestBody Users users) {
        System.out.println("users: " + users);
        Users user = userService.checkUser(users.getUserAccount());
        if (user != null){
            return new ResponseData(400, "用户已存在");
        }else {
            int result = userService.addUser(users);
            return new ResponseData(200, "ok",result);
        }

    }
    @GetMapping("/getVideosInfo")
    public ResponseData getVideoInfo(@RequestParam("page") int page) {
        int pageSize = 6;  // 每页显示12个视频
        List<Video> videoData = videoService.getVideosInfo(page, pageSize);

        // 将返回数据封装为 ResponseData 对象
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);  // 假设 200 为成功状态码
        responseData.setMsg("成功获取视频信息");

        responseData.setData(videoData);

        // 获取 totalCount，确保类型转换正确
        responseData.setCount(1231);  // 将 Long 转换为 int

        return responseData;
    }
    @GetMapping("/getVideoInfo")
    public ResponseData getVideoInfo(@RequestParam("videoId") Long videoId) {
        try {
            Video video = videoService.getVideoInfo(videoId);
            ResponseData responseData = new ResponseData(200,"OK",video,0);
            return responseData;
        } catch (Exception e) {
            return new ResponseData(404,"未查找到",null,0);
        }
    }


    @GetMapping("/addVideo")
    public ResponseData addVideo() {
        boolean isSuccess = videoService.addVideo();  // 调用 service 中的 addVideo 方法插入数据

        ResponseData responseData = new ResponseData();
        if (isSuccess) {
            responseData.setCode(200);  // 成功状态码
            responseData.setMsg("视频信息添加成功");
        } else {
            responseData.setCode(500);  // 错误状态码
            responseData.setMsg("视频信息添加失败");
        }

        return responseData;
    }

    @PostMapping("/addFavoriteVideo")
    public ResponseData addFavoriteVideo(@RequestBody FavoriteVideo favoriteVideo) {
//        return new ResponseData(200, "收藏视频删除成功", favoriteVideo);
        boolean result = videoService.addFavoriteVideo(favoriteVideo);
        if (result) {
            return new ResponseData(200, "收藏视频添加成功", null);
        } else {
            return new ResponseData(400, "收藏视频添加失败", null);
        }
    }

    /**
     * 删除收藏视频
     *
     * @param favoriteVideo 前端传递的收藏视频对象
     * @return ResponseData
     */
    @PostMapping("/delFavoriteVideo")
    public ResponseData delFavoriteVideo(@RequestBody FavoriteVideo favoriteVideo) {
//        return new ResponseData(200, "收藏视频删除成功", favoriteVideo);
        boolean result = videoService.delFavoriteVideo(favoriteVideo);
        if (result) {
            return new ResponseData(200, "收藏视频删除成功", null);
        } else {
            return new ResponseData(400, "收藏视频删除失败", null);
        }
    }

    /**
     * 更新用户信息
     *
     * @param user 前端传递的用户对象
     * @return ResponseData 响应数据
     */
    @PostMapping("/setUser")
    public ResponseData setUser(@RequestBody Users user) {
        boolean result = userService.setUser(user);
        if (result) {
            return new ResponseData(200, "用户信息更新成功", null);
        } else {
            return new ResponseData(400, "用户信息更新失败", null);
        }
    }
    /**
     * 根据 admin_id 获取管理员姓名
     *
     * @param adminId 管理员 ID
     * @return ResponseData 响应数据
     */
    @GetMapping("/getAdminName")
    public ResponseData getAdminName(@RequestParam Integer adminId) {
        String adminName = adminService.getAdminNameById(adminId);
        if (adminName != null) {
            return new ResponseData(200, "管理员信息查询成功", adminName);
        } else {
            return new ResponseData(404, "管理员不存在", null);
        }
    }
    /**
     * 根据用户 ID 数组获取用户信息列表
     *
     * @param userIds 用户 ID 数组
     * @return ResponseData 响应数据，包含用户信息列表
     */
    @PostMapping("/getUsersName")
    public ResponseData getUsersName(@RequestBody List<Long> userIds) {
        List<Users> usersList = userService.getUserNamesByIds(userIds);
        if (usersList != null && !usersList.isEmpty()) {
            return new ResponseData(200, "用户信息查询成功", usersList);
        } else {
            return new ResponseData(404, "未找到相关用户信息", null);
        }
    }


}
